﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using EstateNuke.Data.Properties;

namespace EstateNuke.Data
{
    public partial class Contract
    {
        #region Fileds & Properties

        /// <summary>
        /// </summary>
        public string DisplayName
        {
            get
            {
                return string.Format("{0}{1}", ContractNumber, IsDeleted ? " (已删除)" : "");
            }
        }

        /// <summary>
        /// </summary>
        public IList<ContractReceipt> ContractReceipts
        {
            get
            {
                return InternalContractReceipts.Where(contractReceipt => contractReceipt.IsDeleted == false).ToList();
            }
        }

        #endregion

        #region Add & Remove ContractReceipt

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public ContractReceipt AddContractReceipt(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var contractReceipt = new ContractReceipt
                                      {
                                          RowId = Guid.NewGuid(),
                                          CreatedDate = DateTime.Now,
                                      };
            InternalContractReceipts.Add(contractReceipt);
            contractReceipt.IsDeleted = false;
            contractReceipt.ModifiedDate = DateTime.Now;
            contractReceipt.ModifiedByUser = modifiedUser;
            contractReceipt.ReceiptTypeId = 1;
            contractReceipt.ScheduledDate = DateTime.Now.Date.AddDays(7);
            contractReceipt.ReceivableAmount = 0;
            return contractReceipt;
        }

        /// <summary>
        /// </summary>
        /// <param name = "receiptType"></param>
        /// <param name = "scheduledDate"></param>
        /// <param name = "modifiedUser"></param>
        /// <returns></returns>
        public ContractReceipt AddContractReceipt(ReceiptType receiptType, DateTime scheduledDate, User modifiedUser)
        {
            if (receiptType == null)
            {
                throw new ArgumentNullException("receiptType");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalContractReceipts.Any(item => item.IsDeleted == false && item.ReceiptType == receiptType && item.ScheduledDate == scheduledDate.Date))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.DuplicatedContractReceipt, receiptType.Name, scheduledDate.Date.ToString("yyyy年MM月dd日")));
            }
            var contractReceipt = InternalContractReceipts.FirstOrDefault(item => item.IsDeleted && item.ReceiptType == receiptType && item.ScheduledDate == scheduledDate.Date);
            if (contractReceipt == null)
            {
                contractReceipt = new ContractReceipt
                                      {
                                          RowId = Guid.NewGuid(),
                                          CreatedDate = DateTime.Now,
                                      };
                InternalContractReceipts.Add(contractReceipt);
            }
            contractReceipt.IsDeleted = false;
            contractReceipt.ModifiedDate = DateTime.Now;
            contractReceipt.ModifiedByUser = modifiedUser;
            contractReceipt.ReceiptType = receiptType;
            contractReceipt.ScheduledDate = scheduledDate.Date;
            contractReceipt.ReceivableAmount = 0;
            return contractReceipt;
        }

        /// <summary>
        /// </summary>
        /// <param name = "contractReceipt"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveContractReceipt(ContractReceipt contractReceipt, User modifiedUser)
        {
            if (contractReceipt == null)
            {
                throw new ArgumentNullException("contractReceipt");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalContractReceipts.Contains(contractReceipt))
            {
                contractReceipt.Delete(modifiedUser);
            }
        }

        #endregion

        #region Ensure

        /// <summary>
        /// </summary>
        public void Ensure()
        {
            if (string.IsNullOrEmpty(ContractNumber))
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NotNullAllowed, RowId, "Contract", "ContractNumber"));
            }
            if (ReducedUnitPrice < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "Contract", "ReducedUnitPrice"));
            }
            if (ReducedTotalPrice < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "Contract", "ReducedTotalPrice"));
            }
            if (DiscountRate < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "Contract", "DiscountRate"));
            }
            if (TotalAmount < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "Contract", "TotalAmount"));
            }
            if (FirstInstalmentAmount < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "Contract", "FirstInstalmentAmount"));
            }
            if (MortgageAmount < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "Contract", "MortgageAmount"));
            }
            if (string.IsNullOrEmpty(ContractNumber))
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NotNullAllowed, RowId, "Contract", "ContractNumber"));
            }
            ContractNumber = ContractNumber.Length > 32 ? ContractNumber.Substring(0, 32) : ContractNumber;
            Comment = string.IsNullOrEmpty(Comment) ? null : Comment.Length > 512 ? Comment.Substring(0, 512) : Comment;
        }

        #endregion

        #region Delete

        /// <summary>
        /// </summary>
        public void Delete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = true;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
            ((List<ContractReceipt>) ContractReceipts).ForEach(contractReceipt => contractReceipt.Delete(modifiedUser));
        }

        /// <summary>
        /// </summary>
        public void CancelDelete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = false;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        #endregion
    }
}